home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_04 / pjp / fstream < prev   
Text File  |  1995-03-02  |  4KB  |  132 lines

  1. ------------- Listing 1: The header <fstream> ------------------
  2.  
  3. // fstream standard header
  4. #ifndef _FSTREAM_
  5. #define _FSTREAM_
  6. #include <istream>
  7. #include <ostream>
  8.                 // class filebuf
  9. struct _Filet;
  10. class filebuf : public streambuf {
  11. public:
  12.         filebuf(_Filet *_F = 0)
  13.                 {_Init(_F); }
  14.         filebuf(ios::_Uninitialized)
  15.                 : streambuf(ios::_Noinit) {}
  16.         virtual ~filebuf();
  17.         bool is_open() const
  18.                 {return ((_File != 0)); }
  19.         filebuf *open(const char *, ios::openmode);
  20.         filebuf *open(const char *_N, ios::open_mode _M)
  21.                 {return (open(_N, (ios::openmode)_M)); }
  22.         filebuf *close();
  23. protected:
  24.         virtual int overflow(int = EOF);
  25.         virtual int pbackfail(int = EOF);
  26.         virtual int underflow();
  27.         virtual int uflow();
  28.         virtual streamsize xsgetn(char *, streamsize);
  29.         virtual streamsize xsputn(const char *, streamsize);
  30.         virtual streampos seekoff(streamoff, ios::seekdir,
  31.                 ios::openmode = (ios::openmode)(ios::in | ios::out));
  32.         virtual streampos seekpos(streampos,
  33.                 ios::openmode = (ios::openmode)(ios::in | ios::out));
  34.         virtual streambuf *setbuf(char *, streamsize);
  35.         virtual int sync();
  36.         _Filet *_Init(_Filet * = 0, bool = 0);
  37. private:
  38.         bool _Closef;
  39.         _Filet *_File;
  40.         };
  41.                 // class ifstream
  42. class ifstream : public istream {
  43. public:
  44.         ifstream()
  45.                 : istream(&_Fb) {}
  46.         ifstream(const char *_S, openmode _M = in)
  47.                 : istream(&_Fb) {_Fb.open(_S, _M); }
  48.         virtual ~ifstream();
  49.         filebuf *rdbuf() const
  50.                 {return ((filebuf *)&_Fb); }
  51.         bool is_open() const
  52.                 {return (_Fb.is_open()); }
  53.         void open(const char *_S, openmode _M = in)
  54.                 {if (_Fb.open(_S, _M) == 0)
  55.                         setstate(failbit); }
  56.         void open(const char *_S, open_mode _M)
  57.                 {open(_S, (openmode)_M); }
  58.         void close()
  59.                 {if (_Fb.close() == 0)
  60.                         setstate(failbit); }
  61. private:
  62.         filebuf _Fb;
  63.         };
  64.                 // class ofstream
  65. class ofstream : public ostream {
  66. public:
  67.         ofstream()
  68.                 : ostream(&_Fb) {}
  69.         ofstream(const char *_S, openmode _M = out | trunc)
  70.                 : ostream(&_Fb) {_Fb.open(_S, _M); }
  71.         virtual ~ofstream();
  72.         filebuf *rdbuf() const
  73.                 {return ((filebuf *)&_Fb); }
  74.         bool is_open() const
  75.                 {return (_Fb.is_open()); }
  76.         void open(const char *_S, openmode _M = out | trunc)
  77.                 {if (_Fb.open(_S, _M) == 0)
  78.                         setstate(failbit); }
  79.         void open(const char *_S, open_mode _M)
  80.                 {open(_S, (openmode)_M); }
  81.         void close()
  82.                 {if (_Fb.close() == 0)
  83.                         setstate(failbit); }
  84. private:
  85.         filebuf _Fb;
  86.         };
  87.                 // class stdiobuf
  88. class stdiobuf : public filebuf {
  89. public:
  90.         stdiobuf(_Filet *_F)
  91.                 : filebuf(_F), _Is_buffered(0) {}
  92.         virtual ~stdiobuf();
  93.         bool buffered() const
  94.                 {return (_Is_buffered); }
  95.         void buffered(bool _F)
  96.                 {_Is_buffered = _F; }
  97. private:
  98.         bool _Is_buffered;
  99.         };
  100.                 // class istdiostream
  101. class istdiostream : public istream {
  102. public:
  103.         istdiostream(_Filet *_F)
  104.                 : istream(&_Fb), _Fb(_F) {}
  105.         virtual ~istdiostream();
  106.         stdiobuf *rdbuf() const
  107.                 {return ((stdiobuf *)&_Fb); }
  108.         bool buffered() const
  109.                 {return (_Fb.buffered()); }
  110.         void buffered(bool _F)
  111.                 {_Fb.buffered(_F); }
  112. private:
  113.         stdiobuf _Fb;
  114.         };
  115.                 // class ostdiostream
  116. class ostdiostream : public ostream {
  117. public:
  118.         ostdiostream(_Filet *_F)
  119.                 : ostream(&_Fb), _Fb(_F) {}
  120.         virtual ~ostdiostream();
  121.         stdiobuf *rdbuf() const
  122.                 {return ((stdiobuf *)&_Fb); }
  123.         bool buffered() const
  124.                 {return (_Fb.buffered()); }
  125.         void buffered(bool _F)
  126.                 {_Fb.buffered(_F); }
  127. private:
  128.         stdiobuf _Fb;
  129.         };
  130. #endif /* _FSTREAM_ */
  131.  
  132.